#ifndef QUEUE_
#define QUEUE_

#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <stdio.h>

template <class T> class Queue {
	private:
		key_t	key;
		int		id;

	public:
		Queue ( const char* file, int id );
		~Queue();
		int write ( T data );
		int read ( int type, T* buffer );
};

template <class T> Queue<T> :: Queue ( const char* file, int id ) {
	this->key = ftok ( file, id );
	if ( this->key == -1 )
		perror ( "Error en ftok" );

	this->id = msgget ( this->key, 0666|IPC_CREAT );
	if ( this->id == -1 )
		perror ( "Error en msgget" );
}

template <class T> Queue<T> :: ~Queue () {
	msgctl ( this->id,IPC_RMID,NULL );
}

template <class T> int Queue<T> :: write ( T data ) {
	int resultado = msgsnd ( this->id,(const void *)&data,sizeof(T)-sizeof(long),0 );
	return resultado;
}

template <class T> int Queue<T> :: read ( int type, T* buffer ) {
	int resultado = msgrcv ( this->id,(void *)buffer,sizeof(T)-sizeof(long), type, 0 );
	return resultado;
}

#endif /*QUEUE_*/
